﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Web;
using Engine.DAL;

namespace Engine
{
    public class WebObjectContext
    {
        #region Singleton

        private static readonly WebObjectContext _instance = new WebObjectContext();

        private WebObjectContext()
        {
        }

        public static WebObjectContext Instance
        {
            get
            {
                return _instance;
            }
        }

        #endregion

        public ObjectContext Get()
        {
            var context = HttpContext.Current.Items["ObjectContext"] as ObjectContext;
            if (context == null)
            {
                context = new LibraryEntities();
                HttpContext.Current.Items["ObjectContext"] = context;
            }
            return context;
        }

        public void Dispose()
        {
            var context = HttpContext.Current.Items["ObjectContext"] as ObjectContext;
            if (context != null)
            {
                context.Dispose();
            }
        }
    }
}
